/*:
 * @plugindesc (v1.03)事件对应鼠标指针
 * @author 小c
 * @version 1.00
 * @date 12/3/2021
 *
 * @help 
 * 事件对应鼠标指针
 * 此插件需要CP_CustomMouseCursor支持，请放在他的下面
 *
 * 此插件使玩家指向一个事件时改变鼠标指针的形状。
 * 事件页注释：<Mouse Cursor: name>
 * name为CP_CustomMouseCursor中配置的指针状态名
 * 例：<Mouse Cursor: bat>
 * 当事件在当前页时，鼠标悬停其上，指针会变成bat对应的动画
 *
 * JS函数说明
 * 可复写函数
 * event.mouseCursor() 事件对应的鼠标指针状态名
 * event.isMouseCursorChangeEnabled() 鼠标指向事件时是否变化指针，返回布尔
 * 值，默认事件通过注释配置了指针返回true，否则返回false
 *
 * 更新日志
 * v1.00
 * 插件完成
 *
 * v1.01
 * 删除地图刷新指针的算法，它们被转移至CP_CustomMouseCursor的1.03版
 *
 * v1.02
 * 修复未配置事件鼠标时鼠标指向事件恢复地图默认指针的问题
 * 事件前页已配置的指针不会在后页保留
 *
 * v1.03
 * 此插件可不依赖CP_MouseMove运行
 */

var Imported = Imported || {};
Imported.CP_X_EventMouseCursor = true;

var CP = CP || {};
CP.EventMouseCursor = CP.EventMouseCursor || {};

//------------------------------
// Game_Event
//------------------------------
CP.EventMouseCursor.INIT_EVENT = Game_Event.prototype.initialize;
Game_Event.prototype.initialize = function(mapId, eventId){
	this._mouseCursor = null;

	CP.EventMouseCursor.INIT_EVENT.call(this, mapId, eventId);
};

CP.EventMouseCursor.SETUP_EVENT_PAGE = Game_Event.prototype.setupPage;
Game_Event.prototype.setupPage = function(){
	CP.EventMouseCursor.SETUP_EVENT_PAGE.call(this);

	this.setupMouseCursor();
};

Game_Event.prototype.setupMouseCursor = function(){
	if(!this.page()) return;

	var reg = /<Mouse Cursor: ([\s\S]+?)>/;
	this._mouseCursor = null; //翻页初始化null
	this.list().forEach(function(ev){
		if([108, 408].contains(ev.code)){
			var note = ev.parameters[0];
			if(reg.exec(note))
				this._mouseCursor = RegExp.$1;
		}
	}, this);
};

Game_Event.prototype.mouseCursor = function(){
	return this._mouseCursor;
};

Game_Event.prototype.isMouseCursorChangeEnabled = function(){
	return !!this._mouseCursor;
};

Game_Event.prototype.isMouseOverSelfEvent = function(mouseX, mouseY){
	return mouseX === this.x && mouseY === this.y;
};

//------------------------------
// Game_Map
//------------------------------
//当前鼠标指向的事件
Game_Map.prototype.currentMouseEvent = function(){
	var events = this.events();
	var x = this.canvasToMapX(CP.CustomMouseCursor.mouseX());
	var y = this.canvasToMapY(CP.CustomMouseCursor.mouseY());
	for(var i = 0; i < events.length; i++){
		var event = events[i];
		if(event.isMouseOverSelfEvent(x, y) && event.isMouseCursorChangeEnabled())
			return event;
	}

	return null;
};

CP.EventMouseCursor.CURRENT_MAP_MOUSE_CURSOR = Game_Map.prototype.currentMouseCursor;
Game_Map.prototype.currentMouseCursor = function(){
	var event = this.currentMouseEvent();
	if(!!event)
		return event.mouseCursor();
	else
		return CP.EventMouseCursor.CURRENT_MAP_MOUSE_CURSOR.call(this);
};